<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/1
 * Time: 22:03
 */

namespace Lib\Api;


class AgentApi extends UtilsApi {
    private static $modelName = 'Agent';

    /** 获取经纪人列表
     * @param $where
     * @param $left
     * @param int $page_num
     * @param int $page_size
     * @param string $field
     * @param string $order
     * @return mixed
     */
    public static function getList($where, $left, $page_num = 1, $page_size = 15, $field = "*", $order='') {
        $model = M();
        $list = $model->table('fs_agent a')
            ->join($left)
            ->field($field)
            ->where($where)
            ->order($order)
            ->page($page_num, $page_size)
            ->select();

        return $list;
    }

    public static function getListAll($where, $left='', $field = "*") {
        $model = M(AgentApi::$modelName);
        $list =  $model->field($field)->join($left)->where($where)->select();//page两个参数（页码，条数），不需要自己计算
        return $list;
    }

    public static function getListByCondition($where, $left='', $field = "*") {
        $model = M();
        $list = $model->table('fs_agent a')
            ->join($left)
            ->field($field)
            ->where($where)
            ->select();
        return $list;
    }

    /** 获取经纪人总数
     * @param $where
     * @param $left
     * @return mixed
     */
    public static function getTotal($where, $left) {
        $model = M();
        $total = $model->table('fs_agent a')
            ->join($left)
            ->where($where)
            ->count('a.id');

        return $total;
    }

    /** 获取经纪人总数
     * @param $where
     * @param $left
     * @param $sum
     * @return mixed
     */
    public static function getSum($where, $sum, $left='') {
        $model = M();
        $total = $model->table('fs_agent a')
            ->join($left)
            ->where($where)
            ->sum($sum);

        return $total;
    }

    /** 获取经纪人信息
     * @param $where
     * @param $left
     * @param $field
     * @return mixed
     */
    public static function getInfo($where, $left="", $field= "*") {
        $model = M();
        $agent = $model->table('fs_agent a')
            ->join($left)
            ->field($field)
            ->where($where)
            ->find();

        return $agent;
    }

    /** 添加经纪人
     * @param $data
     * @return mixed
     */
    public static function add($data) {
        $id = parent::add(AgentApi::$modelName, $data);
        return $id;
    }

    /** 修改经纪人
     * @param $data
     * @return bool
     */
    public static function save($data) {
        $state = parent::save(AgentApi::$modelName, $data);
        return $state;
    }

    /** 保存数据
     * @param $where
     * @param $data
     * @return bool
     */
    public static function saveByCondition($where, $data) {
        $model = M(AgentApi::$modelName);
        return $model->where($where)->save($data);
    }

    /** 删除经纪人
     * @param $where
     */
    public static function delete($where) {
        parent::delete(AgentApi::$modelName, $where);
    }
}